package com.cinemtl.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cinemtl.action.UserAction;

/**
 * Servlet implementation class CheckUsername
 */
@WebServlet("/checkusername")
public class CheckUsername extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CheckUsername() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		if (UserAction.checkUsername(username)) {
			String rep = "available";
			try (PrintWriter out = response.getWriter()) {
				out.print(rep);
			}
		} else {
			String rep = "unavailable";
			try (PrintWriter out = response.getWriter()) {
				out.print(rep);
			}
		}

	}
}
